//
//  BaseViewController.m
//  moon
//
//  Created by beyself on 16/6/6.
//  Copyright © 2016年 beyself. All rights reserved.
//
#import "Utils.h"
#import "BaseViewController.h"
#import "MBProgressHUD.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BG;
    
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0) { 
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -5;
    self.navigationItem.leftBarButtonItems = @[negativeSpacer,
                                               [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_back"]
                                                                                style:UIBarButtonItemStylePlain
                                                                               target:self
                                                                               action:@selector(back)]];
}

- (void) back
{
    [self dissmissDialog];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void) showAllTextDialog:(NSString*)msg {
    MBProgressHUD* HUD = [[MBProgressHUD alloc] initWithView:APPDELEGATE.window];
    [APPDELEGATE.window addSubview:HUD];
    HUD.labelText = msg;
//    HUD.color = [UIColor colorWithRed:0 green:0 blue:0 alpha:.8];
    HUD.mode = MBProgressHUDModeCustomView;

    
    [HUD showAnimated:YES whileExecutingBlock:^{
        sleep(1);
    } completionBlock:^{
        
    }];
}

- (void) showWaitDialog {
    if(_waitView)
    {
        [APPDELEGATE.window addSubview:_waitView];
        return;
    }
    
    _waitView = [[UIView alloc] init];
    _waitView.frame = CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT + 64);
    [APPDELEGATE.window addSubview:_waitView];
    
    UIImageView* imageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"load_wait"]];
    imageview.tag = 4321;
    imageview.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 32);
    [_waitView addSubview:imageview];
    /*
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.01];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(endAnimation)];
    imageview.transform = CGAffineTransformMakeRotation(angle * (M_PI / 180.0f));
    [UIView commitAnimations];
    */
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 1;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 100;
    
    [imageview.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    
    /*
    UIActivityIndicatorView* wait = [[UIActivityIndicatorView alloc] init];
    wait.center = CGPointMake(50, 40);
    [subview addSubview:wait];
    [wait startAnimating];
    
    UILabel* label = [[UILabel alloc] init];
    label.font = FONT_12;
    label.textColor = WHITE;
    label.textAlignment = NSTextAlignmentCenter;
    label.frame = CGRectMake(0, 60, 100, 20);
    label.text = @"正在加载 ...";
    [subview addSubview:label];*/
}

- (void) dissmissDialog
{
    if(_waitView && [_waitView superview]) [_waitView removeFromSuperview];
}

- (void) httpCallback:(NSDictionary*)data flag:(int)flag
{
    
}

- (void) httpCallbackFail:(int)flag
{
    
}

- (void) showMsg:(NSString*)msg
{
    UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:nil message:msg
                                                    delegate:nil
                                           cancelButtonTitle:nil
                                           otherButtonTitles:@"确定", nil];
    [dialog show];
}

- (NSString*) getUsername:(NSString*)userId
{
    NSDictionary* dic = (NSDictionary*)[Utils loadCustomObjectWithKey:ALL_MEMBER];
    if(!dic || !dic.count) return @"";
    
    NSDictionary* user = dic[userId];
    if(!user || !user.count) return @"";
    
    NSString* username = user[@"alias"];
    if([Utils isStringNullOrEmpty:username]) username = user[@"username"];
    
    return username;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
